/**
 * @author 王晓伟
 */
 /**
  * 生成表格
  */
var sort = null,bbar="",grid = new Ext.grid.GridPanel();  
function inituserlist(){
		 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";

	Ext.menu.RangeMenu.prototype.icons = {
	  gt: './ajaxFramework/ext-2.1/img/greater_then.png', 
	  lt: './ajaxFramework/ext-2.1/img/less_then.png',
	  eq: './ajaxFramework/ext-2.1/img/equals.png'
	};
	Ext.grid.filter.StringFilter.prototype.icon = './ajaxFramework/ext-2.1/img/find.png';
	var colModel = new Ext.grid.ColumnModel([
		{header:'序号',dataIndex:'orderNo',menuDisabled : true},
		{header:'登录名称',dataIndex:'loginName',menuDisabled : true},
		{header:'用户名称',dataIndex:'userName'},
		{header:'邮件地址',dataIndex:'email',menuDisabled : true},		
		{header:'状态',dataIndex:'state',menuDisabled : true},
		{header:'操作',dataIndex:'operate',menuDisabled : true},
		{header:'userid',dataIndex:'userId',hidden:true},
		{header:'uuid',dataIndex:'uuid',hidden:true}
	]);
	var filters = new Ext.grid.GridFilters({
		 cleanParams: function(p) {  // 清除参数的方法，要保留PagingToolbar分页用的两个参数
		   //var regex = new RegExp("^" + this.paramPrefix + "\[[0-9]+\]");
		   for(var key in p) {
		      if( key != 'start' && key != 'limit' )
		   //if(regex.test(key)) {
		    delete p[key];
		            //}
		               }
          },
	     buildQuery: function(filters) {	
		  // filters 就是getFilterData返回的值, 代码中push入去的对象
		  var p = {};
		  for(var i=0, len=filters.length; i<len; i++) {
		      var f = filters[i];
		      
		      // 取得在GridFilters中定义的filters中的一个filter
		      var filter = this.getFilter( f.field );
		      // 取得传递到server的参数名称
		      var name = (filter.paramName ? filter.paramName : filter.dataIndex);
		      var value = (filter.paramValue ? filter.paramValue : f.data['value']);
		      
		      if( value != '' ) p[ name ] = value;
		  }
		  return p;
		   },	
		  filters:[
		 	{type: 'string',  dataIndex: 'userName'}
	]}); 
	var url = Ext.get('url').getValue();
	sort = new Ext.data.JsonStore({
		url:url,
		fields:['orderNo','loginName','userName','email','state','userId','uuid','operate'],
		root:'root',
		totalProperty:'totalProperty'
	});
	var bbar = new Ext.PagingToolbar({
		pageSize:15,
		store:sort,
		plugins: filters,
		displayInfo:true,
		emptyMsg:'没有记录'
	});
	
   grid = new Ext.grid.GridPanel({
		title:'用户列表',
		cm:colModel,
		renderTo:'topic-grid',
		store:sort,
	//	 frame:true,
		loadMask:true,
		autoHeight:true,
		tbar:[{
			 text: '添加',
             iconCls: 'silk-add',
             handler:function(){
             	window.location.href=Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/addUserForm.do';
             }
		},{
			text: '启用用户列表',
            iconCls: 'silk-qiyong',
            handler:function(){
                window.location.href=Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/qiyongUser.do';
            }
        },{
    		text: '停用用户列表',
            iconCls: 'silk-tiyong',
            handler:function(){
                window.location.href=Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/tingyongUser.do';
           }
        }],
		bbar:bbar,
		plugins: filters,
		viewConfig:{
			forceFit:true,
			enableRowBody:true,
			getRowClass:function(record,rowIndex,p,ds){
				if(record.get('state') == '禁用')
				{
					return 'red-row';
				}
			}
			
		}
	});
	sort.load({params:{start:parseInt(Ext.get('userliststart').getValue()), limit: 15}});	
}
/**
	 * 禁用一个用户
	 * @param {} id 用户ID
	 */
	function enableSystemUser(id){
		 Ext.MessageBox.show({
           title:'消息',
           msg: '是否执行此操作?',
           buttons: Ext.MessageBox.YESNO,
           fn: function(btn){
           	  if(btn == 'yes'){
           	  	    var url = Ext.get('urldisable').getValue();
					Ext.Ajax.request({
						url:url,
						params:{
							id:id
						},
						success:function(response, options){
							    var responseArray = Ext.util.JSON.decode(response.responseText);                                              
			                     if(responseArray.success==true){
			                        Ext.Msg.alert('信息','操作成功！',function(){
			                        	sort.reload({callback :function(){
			                        		grid.getView().refresh();	
			                        	}});										
			                        });			                     
			                     }
			                    else{
			                    	Ext.Msg.alert('信息','操作失败！');     
			                    } 
						}
					});
           	  }	
           },
           icon: Ext.MessageBox.QUESTION
       });
		
					
	}
	
	/**
	 * 重置密码
	 * @param {} id 用户ID
	 */
	function resetPassWord(id){
		 Ext.MessageBox.show({
           title:'消息',
           msg: '是否执行此操作?',
           buttons: Ext.MessageBox.YESNO,
           fn: function(btn){
           	  if(btn == 'yes'){
           	  	    var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/resetPassWord.do';
					Ext.Ajax.request({
						url:url,
						params:{
							id:id
						},
						success:function(response, options){
							    var responseArray = Ext.util.JSON.decode(response.responseText);                                              
			                     if(responseArray.success==true){
			                        Ext.Msg.alert('信息','操作成功！',function(){
			                        	sort.reload({callback :function(){
			                        		grid.getView().refresh();	
			                        	}});										
			                        });			                     
			                     }
			                    else{
			                    	Ext.Msg.alert('信息','操作失败！');     
			                    } 
						}
					});
           	  }	
           },
           icon: Ext.MessageBox.QUESTION
       });
		
					
	}